package com.haha.mining;

import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

import java.awt.*;

/**
 * @author shenlinnan
 * @date 2025/7/27 16:04
 */
public class MiningTest1 implements NativeKeyListener {

    private volatile LoopFind currentLoopFind;

    public static void main(String[] args) throws AWTException {
        try {
            // 配置并注册全局键盘钩子
            GlobalScreen.registerNativeHook();
            GlobalScreen.addNativeKeyListener(new MiningTest1());

            // 禁用JNativeHook日志
            java.util.logging.Logger.getLogger(GlobalScreen.class.getPackage().getName())
                    .setLevel(java.util.logging.Level.OFF);
            System.out.println("配置键盘监听器已注册");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {

    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {

        if (nativeKeyEvent.getKeyCode() == NativeKeyEvent.VC_F9) {
            Point mousePos = MouseInfo.getPointerInfo().getLocation();
            System.out.println("mousePos==" + mousePos);

            stopRunningLoop();

            currentLoopFind = new LoopFind();
            currentLoopFind.setSwitch1(true);
            Thread thread = new Thread(currentLoopFind);
            thread.start();
        }

        if (nativeKeyEvent.getKeyCode() == NativeKeyEvent.VC_F10) {
            System.out.println("按了F10");
            stopRunningLoop();
        }

    }

    /**
     * 停止当前运行的循环
     */
    private void stopRunningLoop() {
        if (currentLoopFind != null) {
            currentLoopFind.setSwitch1(false);
            // 如果需要立即中断
            Thread.currentThread().interrupt();
            currentLoopFind = null;
        }
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {

    }


}
